//
//  LevelScene.cpp
//  BayMaxPuzzle
//
//  Created by Jacky on 15/3/28.
//
//

#include "LoadingScene.h"
#include "MainScene.h"
#include <vector>

USING_NS_CC;
using namespace ui;

Scene* LoadingScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = LoadingScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool LoadingScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    
    for (int i=0; i<3; i++) {
        for (int j=0; j<8; j++) {
            char buffer[50];
            sprintf(buffer,"%d_%d.png", i,j);
            Director::getInstance()->getTextureCache()->addImageAsync(buffer,[=](Texture2D*){
                _picCount++;
                if(_picCount>=_picMax){
                    Director::getInstance()->replaceScene(TransitionSlideInT::create(0.5, MainScene::createScene()));
                }
            });
        }
    }
    
    _picCount = 0;
    _picMax = 24;

    
    auto loadingbk = Sprite::create("loading.png");
    loadingbk->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    loadingbk->setScale(getRealScale(loadingbk->getContentSize().width));
    this->addChild(loadingbk);

    initAnysdk();

    return true;
}

float LoadingScene::getRealScale(int picWidth)
{
    float scale = 0;
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Size designSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
    float originScale = visibleSize.height/designSize.height;
    picWidth *= originScale;
    if(picWidth>visibleSize.width)
    {
        scale = 1;
    }
    else{
        scale = visibleSize.width/picWidth;
    }
    return scale;
}

void LoadingScene::initAnysdk()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    
#endif
}